#include "highscores.h"
#include <QMainWindow>


HighScores::HighScores()
{  
    currentRank = 0;
    bShow = false;
}

void HighScores::load(QString filename)
{
    QFile data(filename);

    if (data.open(QFile::ReadWrite)) {
        QTextStream txt(&data);
        while (!txt.atEnd())
        {
            names.append(txt.readLine());
            scores.append(txt.readLine().toInt());
        }
        data.close();
    }
}

void HighScores::save(QString filename)
{
    QFile data(filename);

    if (data.open(QFile::ReadWrite | QFile::Truncate))
    {
        QTextStream txt(&data);

        for (int i = 0; i<names.length() && i < NUMBER_OF_SCORES ;i++)
        {
            txt << names[i] << endl;
            txt << scores[i] << endl;
        }
        data.close();
     }
}

void HighScores::reset()
{
    names.clear();
    scores.clear();
    currentRank = 0;
}

bool HighScores::isShown()
{
    return bShow;
}

void HighScores::hide()
{
    bShow = false;
}

void HighScores::show()
{
    bShow = true;
}

void HighScores::draw(QPainter *painter)
{
    painter->setPen(QPen(QColor(150,0,0)));
    painter->fillRect(170, 100, 300, 220, Qt::SolidPattern);
    painter->setFont(QFont("Arial", 13));
#ifdef Q_OS_SYMBIAN
    painter->setFont(QFont("Arial", 8));
#endif
    painter->drawText(QRect(170, 110, 300, 25), Qt::AlignCenter, "Highscores:");
#ifdef Q_OS_SYMBIAN
    painter->setFont(QFont("Arial", 7));
#endif
    for (int i = 0; i<scores.length();i++)
    {
        painter->drawText(QRect(185, 135+(i*25), 285, 25), Qt::AlignLeft, QString::number(i+1)+": ");
        painter->drawText(QRect(200, 135+(i*25), 285, 25), Qt::AlignLeft, names[i]);
        painter->drawText(QRect(185+100, 135+(i*25), 285, 25), Qt::AlignCenter, QString::number(scores[i]));
    }

}

bool HighScores::addScore(int score, QWidget *parent)
{
    bool highscore = false;
    int rank =0;

    // Highscore table includes values. Checks the highscore rank
    if (!scores.isEmpty())
    {
        if (scores.length()< (NUMBER_OF_SCORES))
        {
            highscore = true;

        }

        for (rank=0;rank<scores.length();rank++)
        {
            if (score>scores[rank]) {highscore = true;break;}
        }

    }

    // High score achieved. Opens a dialog to ask player's name and then fills the high scores.
    if (scores.isEmpty() || highscore)
    {
        bool ok;
        QString name = QInputDialog::getText(parent, "New Highscore!",
                                                 "Highscore! Enter your name:", QLineEdit::Normal,
                                                 "Player", &ok);
        if(scores.isEmpty()){
            scores.append(score);
            names.append(name);
        }
        else if (scores.length()==NUMBER_OF_SCORES){
            scores.insert(rank, score);
            names.insert(rank, name);
            scores.removeLast();names.removeLast();
        }
        else {
            scores.insert(rank, score);
            names.insert(rank, name);
        }

    }
    else return false;

    return true;
}
